#include "stdafx.h"
#include "Console.h"

Console::Console(HANDLE h)
{
	init(h);
}

Console::Console(E_CONSOLE eConsole)
{
	init(::GetStdHandle(eConsole));
}

Console::~Console()
{
	clear();
}

void Console::clear()
{
	if (hConsole != INVALID_HANDLE_VALUE)
	{
		::CloseHandle(hConsole);
		hConsole = INVALID_HANDLE_VALUE;
	}
}

void Console::init(HANDLE h)
{
	assert(h != INVALID_HANDLE_VALUE);
	if (h != INVALID_HANDLE_VALUE)
	{
		hConsole = h;
	}
}

LPCTSTR Console::get_title()
{
	DWORD Ret = ::GetConsoleTitle(Title, MAX_TITLE);
	assert(Ret > 0);
	return Title;
}

bool Console::set_title( const std::tstring& val )
{
	return ::SetConsoleTitle(val.c_str()) == TRUE;
}

HANDLE Console::get_handle()
{
	return hConsole;
}

DWORD OutputConsole::fill( TCHAR cCharacter, DWORD nLength, COORD dwWriteCoord )
{
	DWORD Ret;
	DWORD Written;

	Ret = ::FillConsoleOutputCharacter(get_handle(), cCharacter, nLength, dwWriteCoord, &Written);
	if (Ret == 0)
	{
		Written = 0;
	}
	return Written;
}

DWORD OutputConsole::fill( TCHAR cCharacter, DWORD nLength, SHORT X, SHORT Y )
{
	COORD coord;
	coord.X = X;
	coord.Y = Y;
	//return fill(cCharacter, nLength, 
	//	(COORD)((X << 16) | Y));
	return fill(cCharacter, nLength, coord);
}

DWORD OutputConsole::write( const void* Buffer, DWORD nLength )
{
	DWORD Ret;
	DWORD Written;

	Ret = ::WriteConsole(get_handle(), Buffer, nLength, &Written, NULL);
	if (Ret == 0)
	{
		Written = 0;
	}
	return Written;
}














